//用户登陆
var QQMapWX = require('../common/qqmap-wx-jssdk.js');
// 实例化API核心类
var qqmapsdk = new QQMapWX({
	key: 'ZHBBZ-2MX6F-MMHJS-J6KN6-NGS5H-TLBWG'
});
module.exports.login = function login(callback) {
	var app = getApp();
	app.loading();
	wx.login({
		success: function (res) {
			// 微信code
			var wxcode = res.code;
			//获取用户基本信息和敏感信息
			wx.getUserInfo({
				withCredentials: true,
				lang: 'zh_CN',
				success: function (res) {
					//用户基本信息
					if (app.globalData.userInfo === null){
						app.globalData.userInfo = res.userInfo;
						wx.setStorageSync('userInfo', res.userInfo);
					}
					
					//服务登陆
					app.apiPost(app.apiList.login, {
						code: wxcode,
						rawData: res.rawData,
						signature: res.signature,
						encryptedData: res.encryptedData,
						vi: res.iv,
					}, function (res) {
						
						if(res.code == 200){
							app.globalData.token = res.data.token;
							wx.setStorageSync('token', res.data.token);
							var city;//用户城市
							if(!res.data.userinfo){
								wx.getLocation({
									type: 'wgs84',
									altitude: false,
									success: function (res) {
										//根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析
										qqmapsdk.reverseGeocoder({
											location: {
												latitude: res.latitude,
												longitude: res.longitude
											},
											success: function (addressRes) {
												city = addressRes.result.address_component.city;
												callback(city);
											}
										})
									},
									fail: function (res) { },
									complete: function (res) { },
								})
							}else{
								city = res.data.userinfo.city.title;
								callback(city);
							}
							app.hideloading();
							
						}
						
					});
					
				}
			})
			
		},
		fail: function (res) {
			console.log('微信登录请求失败')
		},
		// complete: function (res) {}
	})
}

//获取用户信息授权
module.exports.authorize = function authorize() {
	wx.authorize({
		scope: 'scope.userInfo',
		success() {
			console.log('authorize success(scope.userInfo)');
		},
		fail() {
			console.log('authorize fail(scope.userInfo)');
		}
	})
}

//收集、判断系统信息
module.exports.checkSystemInfo = function checkSystemInfo() {
	// 已使用的API
	//------------------
	// wx.getStorageSync v0
	// wx.setStorageSync v0
	//------------------
	// wx.login v0
	// wx.request v0
	// wx.requestPayment v0
	//------------------
	// wx.showModal v0
	// wx.showLoading v1.1.0
	// wx.hideLoading v1.1.0
	//------------------
	// wx.navigateTo v0
	// wx.navigateBack v0
	// wx.reLaunch v1.1.0
	var app = getApp();
	var info = wx.getSystemInfoSync();
	console.log(info);
	//检查微信兼容API
	if (wx.showLoading && wx.hideLoading && wx.reLaunch) {
	} else {
		app.alert('当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。');
	}
	app.globalData.systemInfo = info;
}

//检查页面层级
module.exports.checkPage = function checkPage() {
	var e = getCurrentPages();
	var l = e.length;
	console.log('当前页面层级' + l);
	console.log(e);
}

//上传图片接口
module.exports.uploadImage = function uploadImage(imgLocationUrl, callback){
	var app = getApp();
	var dataJson;
	wx.uploadFile({
		url: app.config.host + app.apiList.uploadImage,
		filePath: imgLocationUrl,
		name: 'file',
		formData: {
			token: app.globalData.token,
			file: imgLocationUrl
		},
		header: { 'content-type': 'multipart/form-data' },
		success: function (res) {
			var dataJson = JSON.parse(res.data);
			dataJson.imgStatus = true;
			callback(dataJson);
		},
		fail: function(res){
			console.log(res);
		}
	})
	
}